package qc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import org.quickconnect.ControlObject;
import org.quickconnect.QuickConnect;

import ui.MainFrame;

import com.mysql.jdbc.Connection;

import controller.ServerConnectionHandler;

public class IsModeratorBCO implements ControlObject{

	@Override
	public Object handleIt(ArrayList<Object> arg0) {
		ServerConnectionHandler connection = (ServerConnectionHandler) arg0.get(0);
		HashMap params = (HashMap) arg0.get(arg0.size()-1);
		Connection con = (Connection) MainFrame.mainFrame.getController().getConnectionPool().getConnection();
		String username = connection.getUser().getUsername();
		String session = (String) params.get("sessionId");
		java.sql.PreparedStatement selectModerator = null;
		ResultSet moderatorResults = null;
		Boolean moderator = true;
		try {
			if(connection.getUser().getRole().equals("USER"))
			{
				selectModerator = con.prepareStatement("SELECT ModeratorID from Moderator m where m.SessionID = (SELECT SessionID FROM Session s WHERE s.SessionNumber = ?) AND m.UserID = (SELECT UserID FROM User u WHERE u.UserName = ?)");
				selectModerator.setString(1, session);
				selectModerator.setString(2, username);
				moderatorResults = selectModerator.executeQuery();
				if(!moderatorResults.next())
				{
					moderator = false;
				}
			}
		} 
		catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		params.put("moderator", moderator);
		return params; 
	}
}